package com.example.caculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	public enum Sign {PLUS, MINUS, MULTIPLE, DIVIDE, EQUAL, POINT};
	Sign S = null;
	
	String inputInfo;
	int CurrentN;
	int InputN;
	
	boolean isPoint = false;
	
	EditText inputWindow;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		inputWindow = (EditText)findViewById(R.id.editText1);
		
		Button clearBtn = (Button)findViewById(R.id.clear);
		Button btn0 = (Button)findViewById(R.id.N0);
		Button btn1 = (Button)findViewById(R.id.N1);
		Button btn2 = (Button)findViewById(R.id.N2);
		Button btn3 = (Button)findViewById(R.id.N3);
		Button btn4 = (Button)findViewById(R.id.N4);
		Button btn5 = (Button)findViewById(R.id.N5);
		Button btn6 = (Button)findViewById(R.id.N6);
		Button btn7 = (Button)findViewById(R.id.N7);
		Button btn8 = (Button)findViewById(R.id.N8);
		Button btn9 = (Button)findViewById(R.id.N9);
		Button divideBtn = (Button)findViewById(R.id.divide);
		Button multipleBtn = (Button)findViewById(R.id.multiple);
		Button minusBtn = (Button)findViewById(R.id.minus);
		Button plusBtn = (Button)findViewById(R.id.plus);
		Button pointBtn = (Button)findViewById(R.id.point);
		Button equalBtn = (Button)findViewById(R.id.equal);
		
		clear();
		
		clearBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				clear();
			}
		});
		
		btn0.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Input(0);
			}
		});
		btn1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Input(1);
			}
		});
		btn2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Input(2);
			}
		});
		btn3.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Input(3);
			}
		});
		btn4.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Input(4);
			}
		});
		btn5.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Input(5);
			}
		});
		btn6.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Input(6);
			}
		});
		btn7.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Input(7);
			}
		});
		btn8.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Input(8);
			}
		});
		btn9.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Input(9);
			}
		});
		
		plusBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CalculateOperate(Sign.PLUS);
				S = Sign.PLUS;
			}
		});
		minusBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CalculateOperate(Sign.MINUS);
				S = Sign.MINUS;
			}
		});
		multipleBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CalculateOperate(Sign.MULTIPLE);
				S = Sign.MULTIPLE;
			}
		});
		divideBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CalculateOperate(Sign.DIVIDE);
				S = Sign.DIVIDE;
			}
		});
		equalBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CalculateOperate(Sign.EQUAL);
				S = Sign.EQUAL;
			}
		});
		pointBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isPoint = true;
				CalculateOperate(Sign.POINT);
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void clear(){
		CurrentN = 0;
		InputN = 0;
		inputInfo = "";
		inputWindow.setText("");
	}
	
	public void Input(int n){
		InputN = 10*InputN + n;
		
    	inputInfo = inputWindow.getText().toString();
    	inputWindow.setText(inputInfo + n);
    }
	
	public void CalculateOperate(Sign s){
		
		if(s == Sign.PLUS){
			GetInput("+");
			CurrentN += InputN;
			InputN = 0;
		}else if(s == Sign.MINUS){
			GetInput("-");
			CurrentN -= InputN;
			InputN = 0;
		}else if(s == Sign.MULTIPLE){
			GetInput("X");
			if(CurrentN == 0){
				CurrentN = InputN;
			}else{
				CurrentN *= InputN;
			}
				InputN = 0;
		}else if(s == Sign.DIVIDE){
			GetInput("/");
			if(CurrentN == 0){
				CurrentN = InputN;
			}else{
				CurrentN /= InputN;
			}
			InputN = 0;
		}else if(s == Sign.EQUAL){
			isPoint = false;
			if(S == Sign.PLUS) CurrentN += InputN;
			else if(S == Sign.MINUS) CurrentN -= InputN;
			else if(S == Sign.DIVIDE) CurrentN /= InputN;
			else if(S == Sign.MULTIPLE) CurrentN *= InputN;
			
			InputN = 0;
			inputWindow.setText("" + CurrentN);
			
		}else if(s == Sign.POINT){
			GetInput(".");
		}
	}
	
	public void GetInput(String str){
		inputInfo = inputWindow.getText().toString();
    	inputWindow.setText(inputInfo + str);
	}
	
}